package cn.com.guage.dtm.design.pattern.factory.dynamics;// 动态支付工厂类
import java.util.HashMap;
import java.util.Map;
// 支付接口
interface Payment {
    void pay(double amount);
}

// 支付宝支付实现类
class Alipay implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("使用支付宝支付了 " + amount + " 元");
    }
}

// 微信支付实现类
class WechatPay implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("使用微信支付了 " + amount + " 元");
    }
}

// 银行卡支付实现类
class BankCardPay implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("使用银行卡支付了 " + amount + " 元");
    }
}

class DynamicPaymentFactory {
    private static final Map<String, Class<? extends Payment>> paymentMap = new HashMap<>();

    static {
        paymentMap.put("alipay", Alipay.class);
        paymentMap.put("wechatpay", WechatPay.class);
        paymentMap.put("bankcardpay", BankCardPay.class);
    }

    public static Payment createPayment(String paymentType) {
        try {
            Class<? extends Payment> paymentClass = paymentMap.get(paymentType.toLowerCase());
            if (paymentClass == null) {
                throw new IllegalArgumentException("不支持的支付类型: " + paymentType);
            }
            return paymentClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("创建支付对象失败: " + e.getMessage(), e);
        }
    }
}

// 主类
public class 动态工厂模式_支付实现 {
    public static void main(String[] args) {
        // 创建支付宝支付对象并支付
        Payment alipay = DynamicPaymentFactory.createPayment("alipay");
        alipay.pay(100.0);

        // 创建微信支付对象并支付
        Payment wechatPay = DynamicPaymentFactory.createPayment("wechatpay");
        wechatPay.pay(200.0);

        // 创建银行卡支付对象并支付
        Payment bankCardPay = DynamicPaymentFactory.createPayment("bankcardpay");
        bankCardPay.pay(300.0);
    }
}
    